let BKTools = require("BKTools");
let Global = require("Global");
cc.Class({
    extends: cc.Component,

    properties: {
        imgNode: {
            default: null,
            type: cc.Node,
            tooltip: "图片节点",
            visible: true
        },
        count: {
            get() {
                if (this._count === undefined) {
                    this._count = 0;
                }
                return this._count;
            },
            set(value) {
                this._count = value;
            }
        },
        tipNode: {
            default: null,
            type: cc.Node,
            tooltip: "提示节点",
            visible: true
        },
        timeLabel: {
            default: null,
            type: cc.Label,
            tooltip: "现在时间",
            visible: true
        }
    },

    onBtnClick(event, data) {
        let self = this;
        if (data == 'makeDir') {
            if (!Global.isWeb) {

                let isOk = BKTools.makeDir("GameSandBox://test");
                cc.log("GameSandBox文件夹是否创建成功.." + isOk);
                //无法在根目录下创建文件以及文件夹
                isOk = BKTools.makeDir("GameRes://test");
                cc.log("文件夹是否创建成功.." + isOk);
            } else {
                cc.log("Is web browser");
                self.setTipMsg("请在QQ玩一玩环境下测试");
            }
        } else if (data == 'changeImg') {
            self.changeImg();
        } else if (data == 'post') {
            //网络请求
            BKTools.get("http://www.wanandroid.com/tools/mockapi/3461/Javen", "", function (status, data) {
                cc.log("status>" + status + " data>" + data);
                if (status == 200) {
                    if (data.code == 0) {
                        self.setTipMsg("网络请求结果 Gitee:" + data.data.name);
                        cc.log(JSON.stringify(data));
                    } else {
                        self.setTipMsg("网络请求异常:" + data.msg);
                    }
                }
            });
        } else if (data == 'loadSubpackage') {
            if (CC_PREVIEW || CC_QQPLAY) {
                self.setTipMsg("无法预览加载子包");
                return;
            }
            cc.loader.downloader.loadSubpackage('subpackage', err => {
                if (err) {
                    console.error(err);
                    self.setTipMsg("subpackage.failed");
                    return;
                }
                console.log('load subpackage successfully.');
                cc.director.loadScene('subpackage');
            });
        } else if (data == 'progress') {
            cc.director.loadScene("slider");
        } else if (data == 'scratchCard') {
            cc.director.loadScene("scratchCard");
        } else if (data == 'skipGame') {
            cc.log("cc.sys.platform>" + cc.sys.platform);
            if (cc.sys.platform != cc.sys.QQ_PLAY) {
                self.setTipMsg("请在QQ玩一玩环境下测试");
                return;
            }
            if (BKTools.versionCompare(GameStatusInfo.QQVer, "7.7.0.0")) {
                BKTools.skipGame("2731");
            } else {
                self.setTipMsg("手Q版本过低,请更新");
            }
        } else if (data == 'follow') {
            if (cc.sys.platform == cc.sys.QQ_PLAY) {
                cc.log("follow.....");
                BKTools.follow();
            } else {
                self.setTipMsg("请在QQ玩一玩环境下测试");
            }
        } else if (data == 'toShare') {
            if (cc.sys.platform == cc.sys.QQ_PLAY) {
                cc.log("toShare.....");
                BKTools.toShare(BKTools.getShareInfo(), function (code) {
                    cc.log("分享结果:" + code);
                });
                cc.log("fffff....");
            } else {
                self.setTipMsg("请在QQ玩一玩环境下测试");
            }
        } else if (data == 'loadImage') {
            if (cc.sys.platform == cc.sys.QQ_PLAY) {
                //下载图片并保存在手机中
                BKTools.BKGet("http://h.hiphotos.baidu.com/image/pic/item/18d8bc3eb13533fa4dd573ada3d3fd1f40345bd6.jpg", function (res, code) {
                    cc.log("结果:" + code + " 渗透参数:" + this.custom);
                    BK.FileUtil.writeBufferToFile("GameSandBox://test/test.jpg", res);
                }, "custom");
            } else {
                self.setTipMsg("请在QQ玩一玩环境下测试");
            }

        } else if (data == 'bkNet') {
            if (cc.sys.platform == cc.sys.QQ_PLAY) {
                BKTools.BKGet("http://www.wanandroid.com/tools/mockapi/3461/Javen", function (res, code) {
                    cc.log("结果:" + code + " 渗透参数:" + this.custom);
                    if (code == 200) {
                        let str = res.readAsString();
                        cc.log(str);
                        let data = JSON.parse(str);
                        if (data.code == 0) {
                            self.setTipMsg("网络请求结果 Gitee:" + data.data.name);
                        } else {
                            self.setTipMsg("网络请求异常:" + data.msg);
                        }
                    }
                }, "请求返回字符串");
            } else {
                self.setTipMsg("请在QQ玩一玩环境下测试");
            }
        } else if (data == 'webSocket') {
            // BKTools.loadImage("http://h.hiphotos.baidu.com/image/pic/item/18d8bc3eb13533fa4dd573ada3d3fd1f40345bd6.jpg",function(url){
            //     cc.log(url);
            // })
            //需要全局设置webSocket请求的host
            cc.director.loadScene("webSocket");
        } else if (data == 'head') {
            cc.director.loadScene("head");
        } else if (data == 'toAd') {
            cc.director.loadScene("ad");
        }
    },

    changeImg() {
        let self = this;

        if (self.count > 4) {
            self.count = 0;
        }
        cc.log("切换图片:" + self.count);
        if (self.count == 0) {
            self.loadUrl("http://h.hiphotos.baidu.com/image/pic/item/18d8bc3eb13533fa4dd573ada3d3fd1f40345bd6.jpg");
        } else if (self.count == 1) {
            self.loadUrl("http://a.hiphotos.baidu.com/image/pic/item/d000baa1cd11728bfffcbac4c3fcc3cec2fd2c06.jpg");
        } else if (self.count == 2) {
            self.loadUrl("http://e.hiphotos.baidu.com/image/pic/item/bf096b63f6246b6068265ef7e0f81a4c510fa20d.jpg");
        } else if (self.count == 3) {
            if (cc.sys.platform == cc.sys.QQ_PLAY) {
                self.loadRes();
            } else {
                self.setTipMsg("QQ玩一玩才能加载此图片");
            }
        } else if (self.count == 4) {
            if (cc.sys.platform == cc.sys.QQ_PLAY) {
                self.loadLocal("GameRes://test.png");
            } else {
                self.setTipMsg("QQ玩一玩才能加载此图片");
            }
        }
        self.count += 1;
    },

    onLoad() {
        let self = this;
        cc.director.setDisplayStats(false);
        Global.isWeb = cc.sys.isBrowser;
        cc.log("Global.isWeb:" + Global.isWeb);

        if (cc.sys.platform == cc.sys.QQ_PLAY) {
            BKTools.addGameEvent();
            BKTools.log("已添加QQ玩一玩游戏事件以及生命周期");
        }

        self.loadRes();

        self.setTipMsg("请点击按钮测试");
        if (cc.sys.platform != cc.sys.QQ_PLAY) {
            self.schedule(function () {
                let data = new Date();
                let y = data.getFullYear();
                let m = data.getMonth();
                m = m < 10 ? 0 + "" + m : m;
                let d = data.getDate();
                d = d < 10 ? 0 + "" + d : d;
                let h = data.getHours();
                h = h < 10 ? 0 + "" + h : h;
                let mm = data.getMinutes();
                mm = mm < 10 ? 0 + "" + mm : mm;
                let s = data.getSeconds();
                s = s < 10 ? 0 + "" + s : s;

                // cc.log(data.getMilliseconds()); //获取当前毫秒数(0-999)
                // cc.log(data.toLocaleDateString()); //获取当前日期 2018/9/4
                // cc.log(data.toLocaleTimeString());//获取当前时间 下午5:20:30
                // cc.log(data.toLocaleString()); //获取日期与时间 2018/9/4 下午5:20:30

                self.timeLabel.string = y + "-" + m + "-" + d + " " + h + ":" + mm + ":" + s;
            }, 1);
        }


        cc.log("CC_QQPLAY:" + CC_QQPLAY + " CC_JSB:" + CC_JSB);

        BKTools.log("onload end ...");
    },
    /**
     * 动态加载图片,必须放置在 resources 文件夹或它的子文件夹下
     * 如果一份资源不需要由脚本直接动态加载，那么千万不要放在 resources 文件夹里。
     */
    loadRes() {
        let self = this;
        cc.loader.loadRes("img", cc.SpriteFrame, function (error, resources) {
            if (error == null) {
                self.setImg(resources);
            }
        });
    },
    loadUrl(remoteUrl) {
        let self = this;
        cc.loader.load(remoteUrl, function (err, texture) {
            if (err == null) {
                let spriteFrame = new cc.SpriteFrame(texture);
                self.setImg(spriteFrame);
            }
        });
    },
    loadLocal(absolutePath) {
        let self = this;
        cc.loader.load(absolutePath, function (err, texture) {
            if (err == null) {
                let spriteFrame = new cc.SpriteFrame(texture);
                self.setImg(spriteFrame);
            }
        });
    },

    setImg(spriteFrame) {
        this.imgNode.getComponent(cc.Sprite).spriteFrame = spriteFrame;
    },

    setTipMsg(msg) {
        let lable = this.tipNode.getComponent(cc.Label);
        lable.string = msg;
        this.scheduleOnce(function () {
            lable.string = "请点击按钮测试";
        }, 3);
    },

    start() {

    },

    // update (dt) {},
});